﻿(function () {
    $(function () {

        var _dynamicInfomationService = abp.services.app.dynamicInfomation;

        //初始化分页
        $.paginationInit($('.pagination'), '', 7);

        //项目动态


        var _$dynamicInfoModal = $('#DynamicInfoCreateModal');
        var _$dynamicInfoform = _$dynamicInfoModal.find('form');

        $('.add-dynamicinfo').on('click', function () {
            var projectId = $(this).attr('data-project-id');
            _$dynamicInfoform.find('input[name="projectId"]').val(projectId);
        });

        _$dynamicInfoform.find('.add-resource').click(function () {
            var template = _$dynamicInfoform.find('.add-resource-template').html();
            var $container = _$dynamicInfoform.find('.add-resource-container');
            var $item = $(template);

            $item.find('.btn-select-resource').on('click', function () {
                $item.find('input[type="file"]').on('change', function () {
                    $item.find('input[type="text"]').val($(this).val());
                }).click();
            });

            $item.find('.btn-remove-resource').on('click', function () {
                var _$item = $item;
                _$item.remove();
            });
            $container.append($item);
        });

        _$dynamicInfoform.find('button[type="submit"]').click(function (e) {
            e.preventDefault();
            if (!_$dynamicInfoform.valid()) {
                return;
            }

            var dynamicInfo = _$dynamicInfoform.serializeFormToObject(); //serializeFormToObject is defined in main.js

            //上传文件
            var resourceIds = resourceUpload();
            dynamicInfo.ResourceIds = resourceIds;

            var _dynamicInfomationService = abp.services.app.dynamicInfomation;

            abp.ui.setBusy(_$dynamicInfoModal);
            _dynamicInfomationService.addDynamicInfomation(dynamicInfo).done(function () {
                _$dynamicInfoModal.modal('hide');
                location.reload(true); //reload page to see new user!
            }).always(function () {
                abp.ui.clearBusy(_$dynamicInfoModal);
            });
        });

        function resourceUpload() {
            var $inputs = _$dynamicInfoform.find('.add-resource-container input[type="file"]');
            var formData = new FormData();
            $inputs.each(function () {
                formData.append('resource-' + new Date().getTime(), this.files[0]);
            });
            var ids = [];
            $.ajax({
                type: 'post'
                , url: abp.appPath + 'api/services/app/Resources/Post'
                , cache: false
                , dataType: 'JSON'
                , processData: false
                , contentType: false
                , data: formData
                , async: false
            }).done(function (res) {
                console.log(res);
                $.each(res.result.resources, function () {
                    ids.push(this.id);
                });
            });
            return ids;
        }

        //异步载入资源
        $('.panel-body-information-details').each(function () {
            var informationId = $(this).attr('data-information-id');
            var $othis = $(this);
            var $loading = $othis.find('.resource-loading');
            //abp.ui.setBusy($loading);
            _dynamicInfomationService.getInfomationResourcesByInfomationId({ id: informationId }).done(function (res) {
                var $_othis = $othis;
                if (res.length > 0) {
                    var template = $('#resource-templte').html();
                    var $ul = $(template);
                    var liTemplate = $ul.html();
                    $ul.html('');

                    $.each(res, function () {
                        var $li = $(liTemplate);
                        $li.on('click', function () {
                            var resourceId = $li.attr('data-resource-id');
                            window.open(abp.appPath + 'api/services/app/Resources/Get?resourceId=' + resourceId);
                        });
                        $li.attr('data-resource-id', this.resource.id);
                        $li.find('.resource-name').text(this.resource.name);
                        $ul.append($li);
                    });
                    $_othis.append($ul);
                }
                abp.ui.clearBusy($loading);
            });
        });

    });
})();
